package com.shuai.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.shuai.stickynote.R;
import com.shuai.stickynote.TextNote;
import com.shuai.tool.MyTime;

import java.util.ArrayList;

public class TextListAdapter extends RecyclerView.Adapter<TextListAdapter.BaseViewHolder> {
    private ArrayList<TextNote> dataList;
    private OnRecyItemClickListener onRecyItemClickListener;
    private OnRecyItemLongClickListener onRecyItemLongClickListener;
    public interface OnRecyItemClickListener{
        void onClick(View view, int position);
    }
    public interface OnRecyItemLongClickListener{
        void onClick(View view, int position);
    }
    public void setOnRecyItemClickListener(OnRecyItemClickListener onRecyItemClickListener) {
        this.onRecyItemClickListener = onRecyItemClickListener;
    }
    public void setOnRecyItemLongClickListener(OnRecyItemLongClickListener onRecyItemLongClickListener) {
        this.onRecyItemLongClickListener = onRecyItemLongClickListener;
    }

    public void replaceAll(ArrayList<TextNote> data){
        dataList = new ArrayList<>();
        dataList.clear();
        if (data != null && data.size() > 0){
            dataList.addAll(data);
        }
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new OneViewHolder(LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_text_list,parent,false));
    }
    @Override
    public void onBindViewHolder(@NonNull final BaseViewHolder holder, int position) {
        holder.setData(dataList.get(position),position);
        if (onRecyItemClickListener != null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int i = holder.getLayoutPosition();
                    onRecyItemClickListener.onClick(holder.itemView,i);
                }
            });
        }
        if (onRecyItemLongClickListener != null){
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int i = holder.getLayoutPosition();
                    onRecyItemLongClickListener.onClick(holder.itemView,i);
                    return true;
                }
            });
        }
    }
    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public class BaseViewHolder extends RecyclerView.ViewHolder{
        public BaseViewHolder(@NonNull View itemView) {
            super(itemView);
        }
        void setData(TextNote note, int position){

        }
    }

    private class OneViewHolder extends BaseViewHolder{
        private TextView tv_title,tv_content,tv_time;
        private float ly_height;
        public OneViewHolder(@NonNull View itemView) {
            super(itemView);
            ly_height = itemView.getHeight();
            tv_title = itemView.findViewById(R.id.item_text_title);
            tv_content = itemView.findViewById(R.id.item_text_content);
            tv_time = itemView.findViewById(R.id.item_text_time);
        }

        @Override
        void setData(TextNote note, int position) {
            if (note != null){
                tv_title.setText(note.getTitle());
                tv_content.setText(note.getContent());
                tv_time.setText(MyTime.toDateString(note.getTime()));
            }
        }
    }

}
